package com.mike.billingserver.service.impl;

import com.mike.billingserver.entity.User;
import com.mike.billingserver.form.UserForm;
import com.mike.billingserver.repository.UserRepository;
import com.mike.billingserver.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @author xiaozefeng
 * @date 2018-12-18 16:28
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public UserForm createUser(UserForm userForm) {
        String password = userForm.getPassword();
        String hexPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        User user = new User();
        BeanUtils.copyProperties(userForm, user);
        user.setPassword(hexPassword);
        user.setName(userForm.getUsername());
        userRepository.save(user);
        userForm.setId(user.getId());
        return userForm;
    }
}
